import { createRouter, createWebHashHistory } from "vue-router"
import LS from '@/plugins/localstorage'

const Login = () => import("@/views/Login.vue")

const Basic = () => import("@/views/layouts/Basic.vue")
const Home = () => import("@/views/Home.vue")
const About = () => import("@/views/About.vue")
const AccountSettings = () => import("@/views/AccountSettings.vue")
// 文档
const DocumentIndex = () => import("@/views/document/Index.vue")
const DocumentCreate = () => import("@/views/document/Create.vue")
// 设备
const Terminal = () => import("@/views/Terminal.vue")
const TerminalModel = () => import("@/views/TerminalModel.vue")
// 聊天记录
const RecordIndex = () => import("@/views/Record/Index.vue")

const routes = [
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  { 
    path: "/", 
    component: Basic,
    children: [
      { path: '', redirect: '/home' },
      {
        path: 'home',
        name: 'home',
        component: Home,
      },
      {
        path: "about",
        name: "about",
        component: About,
      },
      // account-settings
      {
        path: "account-settings",
        name: "account.settings",
        component: AccountSettings,
      },
      // document 文档
      {
        path: "document",
        name: "document",
        component: DocumentIndex,
      },
      {
        path: "document/create",
        name: "document.create",
        component: DocumentCreate,
      },
      {
        path: "document/:id/edit",
        name: "document.edit",
        component: DocumentCreate,
      },
      // 设备
      {
        path: "terminal",
        name: "terminal",
        component: Terminal,
      },
      {
        path: "termodel",
        name: "termodel",
        component: TerminalModel,
      },
      // 聊天记录
      {
        path: "chatgpt-record",
        name: "chatgpt.record",
        component: RecordIndex,
      },
    ]
  },
  
]
const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const token = LS.get('token')
  // console.log('token value: ', token)

  if (to.name !== 'login' && !token) next({ name: 'login' })
  else next()
})

export default router
